package com.pingancar.creditmanage.service;

import com.pingancar.creditmanage.dao.OrderitemDaoImpl;
import com.pingancar.creditmanage.dao.PAServiceDaoImpl;
import com.pingancar.creditmanage.dao.ShopInfoDaoImpl;
import com.pingancar.creditmanage.dao.UserDaoImpl;
import com.pingancar.creditmanage.pojo.OrderitemPojo;
import com.pingancar.creditmanage.pojo.PaservicePojo;
import com.pingancar.creditmanage.pojo.ShopinfoPojo;
import com.pingancar.creditmanage.pojo.UserPojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * Created by SilverStone-WW on 2015/2/11.
 */
@Service
public class UserOrderService {
    @Autowired
    OrderitemDaoImpl orderitemDao;
    @Autowired
    UserDaoImpl userDao;
    @Autowired
    PAServiceDaoImpl paServiceDao;
    @Autowired
    ShopInfoDaoImpl shopInfoDao;

    /**
     * 添加订单
     */
    public OrderitemPojo addOrder(Integer userid, Integer paserviceId) {
        PaservicePojo paservicePojo = paServiceDao.load(paserviceId);
        if (paservicePojo == null)
            return null;

        /* 扣除积分 */
        UserPojo user = userDao.decreaseCredit(userid, paservicePojo.getCreditcost());

        /* 积分不足 */
        if (user == null)
            return null;

        OrderitemPojo orderitem = new OrderitemPojo();
        orderitem.setPaservice(paservicePojo);
        orderitem.setUser(user);
        orderitem.setOrdertime(new Timestamp(new Date().getTime()));
        orderitem.setStatus(OrderitemPojo.ORDERING);
        orderitem.setOrderitemid(new Date().getTime() + "" + (int)(Math.random()*1000));
        orderitem.setCredit(paservicePojo.getCreditcost());

        orderitemDao.save(orderitem);

        return orderitem;
    }

    /**
     * 查询订单列表
     * @param userPojo
     * @return
     */
    public List<OrderitemPojo> getOrderlistByUser(UserPojo userPojo, Integer offset, Integer length, Integer status) {
        return orderitemDao.getOrderlistByUser(userPojo, offset, length, status);
    }

    public OrderitemPojo rejectOrder(Integer id, Integer userid) {

        OrderitemPojo order = orderitemDao.load(id);
        if (order == null || order.getStatus() != OrderitemPojo.ORDERING
                || order.getUser().getId() != userid)
            return null;

        UserPojo user = userDao.increaseCredit(userid, order.getCredit());

        order.setUser(user);
        order.setStatus(OrderitemPojo.CANCELED);
        orderitemDao.update(order);

        return order;
    }

    public boolean commentOrder(Integer id, Integer rate, String comment) {
        OrderitemPojo orderitemPojo = orderitemDao.load(id);
        if (orderitemPojo == null)
            return false;

        ShopinfoPojo shopinfoPojo = orderitemPojo.getPaservice().getShopinfo();
        if (shopinfoPojo == null)
            return false;

        orderitemPojo.setStatus(OrderitemPojo.COMMENT);
        orderitemPojo.setStar(rate);
        orderitemPojo.setComment(comment);
        orderitemDao.update(orderitemPojo);

        shopinfoPojo.setRatesum(shopinfoPojo.getRatesum()+1);
        shopinfoPojo.setStarsum(shopinfoPojo.getStarsum()+rate);

        shopInfoDao.update(shopinfoPojo);

        return true;
    }

}
